package priorityqueue;

public class MyPriorityQueue {
    private int [] array;

    private int size = 0;

    public MyPriorityQueue(){
        array = new int[1000];
    }

    private void offer(int value){
        //先把新元素尾插,再向下调整
        if(size == array.length){
            array = new int[(int)(size * 1.5)];
        }
        array[size] = value;
        size++;
        //从最后一个元素的位置开始进行向上调整
        Heap.shiftUp(array,size - 1);
    }
    public Integer poll(){
        if(size == 0){
            return null;
        }
        int ret = array[0];
        array[0] = array[size - 1];
        array[size - 1] = ret;
        size--;
        //从第一个元素开始进行向下调整
        Heap.shiftDown(array,size,0);
        return ret;
    }

    public Integer peek(){
        if(size == 0){
            return null;
        }
        return array[0];
    }

    public static void main(String[] args) {
        MyPriorityQueue queue = new MyPriorityQueue();
        queue.offer(3);
        queue.offer(1);
        queue.offer(4);
        queue.offer(2);
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());

    }
}
